package com.grablist.feihu.grablist.ui.actviity.login.presenter;

import android.text.TextUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import com.grablist.feihu.grablist.base.BaseBiz;
import com.grablist.feihu.grablist.event.RechargeEvent;
import com.grablist.feihu.grablist.ui.actviity.login.biz.CheckBiz;
import com.grablist.feihu.grablist.ui.actviity.login.contract.CheckContract;
import com.grablist.feihu.grablist.util.FileUtils;

import org.greenrobot.eventbus.EventBus;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/10/21.
 */

public class CheckPresenter implements CheckContract.Presenter {



    CheckContract.View view;
    CheckBiz checkBiz;


    @Inject
    public CheckPresenter(CheckContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        checkBiz=(CheckBiz)biz;

    }
    @Override
    public void upload( String  code,String realName, String companyName, String companyTel,List<String> photoInfos) {

        if (TextUtils.isEmpty(realName)) {
            view.showMsg("真实姓名没有填写");
            return;
        }
        if (TextUtils.isEmpty(companyName)) {
            view.showMsg("公司名字未填写");
            return;
        }

        if (TextUtils.isEmpty(companyTel)) {
            view.showMsg("公司电话为填写");
            return;
        }
        if(photoInfos.size()==0){
            view.showMsg("选择认证图片");
            return;
        }
        view.loading(true);
        Map<String, RequestBody> params = new HashMap<>();
        params.put("userCode", FileUtils.convertToRequestBody(code));
        params.put("realName", FileUtils.convertToRequestBody(realName));
        params.put("companyName", FileUtils.convertToRequestBody(companyName));
        params.put("companyTel", FileUtils.convertToRequestBody(companyTel));


        List<File> fileList = new ArrayList<>();
        for (int i = 0; i < photoInfos.size(); i++) {
            File file = new File(photoInfos.get(i));//filePath 图片地址
            fileList.add(file);
        }

        List<MultipartBody.Part> partList = FileUtils.filesToMultipartBodyParts(fileList);
        checkBiz.setInfo(params, partList, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object o) {
                view.loading(false);
                view.showMsg("申请认证成功");
                EventBus.getDefault().post(new RechargeEvent());
                view.finishAct();
            }

            @Override
            public void onFailure(Object o) {
                view.showMsg("申请认证失败");
                view.loading(false);
            }
        });
    }



}
